# frozen_string_literal: true
# == Schema Information
#
# Table name: users
#
#  id              :integer          not null, primary key
#  village_id      :integer
#  building_id     :integer
#  name            :string
#  telephone       :string
#  id_card         :string
#  floor_code      :string
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#  password_digest :string
#  token           :string
#  registration_id :string
#  avatar          :string
#  car_code        :string
#  state           :boolean          default(FALSE)
#  address         :string
#  number          :string
#
# Indexes
#
#  index_users_on_building_id  (building_id)
#  index_users_on_village_id   (village_id)
#

class User < ApplicationRecord
  mount_uploader :avatar, AvatarUploader
  belongs_to :building#,optional: true
  belongs_to :village#,optional: true
  has_many :interviews

  has_secure_password

  has_secure_token

  scope :keyword, ->(keyword) do
    if keyword.blank?
      all
    else
      where("floor_code LIKE ? ", "%#{keyword}%")
    end
 end

  before_validation :init_password, on: :create

 protected
    def init_password
      self.password  = '123456'
    end
end
